import { Controller, Get, Post, Request,Param, Query, Inject,UseGuards, Body} from '@nestjs/common';
// import { ConfigModule } from '../config/config.module';
import {AuthGuard} from "@nestjs/passport"

import { jwtService } from "../config/config.service"
import { MyService } from "./my.service"
import {utils} from "../config/config.utils"
// import {StaffService} from "./staff.service"
// import { StaffService } from './staff.service';
@Controller('myService')
export class MyController {
    constructor(
       @Inject("jwt") private friend: jwtService,
       @Inject("utils") private utils:utils,
    //    @Inject() private staffService:StaffService,
        // private staffService:StaffService,
        private myService: MyService
    ) { }

    //用户注册
    @Post("register")
    register(@Body() body) {
     
        let userInfo = {
            phone: body.phone,
            password: body.password,
            username: body.username,
            avator: body.avator.length == 0 ? "1675933153281.jpg" : body.avator,
            class:body.class,
            team:body.team
        }

        // return userInfo

        return this.myService.register(userInfo)
    }

    //删除用户
    @Get("deleteUser")
    async deleteUser(@Query() que) {

        try {
            let res = await this.myService.deleteUser(que.id)

            if (res.affected > 0) {
                return {
                    code: 200,
                    mes: "删除成功"
                }
            } else {
                return {
                    code: 400,
                    mes: "删除失败"
                }
            }
        } catch (err) {
            return {
                code: 400,
                mes: err
            }
        }


    }

    // 修改用户
    @Get("updateUser")
    uodateUser(@Query() que) {
        let id = parseInt(que.id)
        //    return  this.myService.updateUser(id)
        if (this.myService.updateUser(id)) {
            return {
                code: 200,
                mes: "更新成功"
            }
        } else {
            return {
                code: 400,
                mes: "更新失败"
            }
        }
    }

    // 用户登录
    @Get("login")
    login(@Query() que) {
        // console.log(que)
        // let id = parseInt(que.id)
        // return {
        //     que,
        //     code:200
        // }
        let password = que.password
        let phone = que.phone
        return this.myService.login(password, phone)
    }

       // 用户名登录
       @Get("namelogin")
       namelogin(@Query() que) {
           // console.log(que)
           // let id = parseInt(que.id)
           // return {
           //     que,
           //     code:200
           // }
           let username = que.username
           let password = que.password
           return this.myService.namelogin(username, password)
       }


    // 获取用户数据
    @Get("getUser")
    getUser(@Query() que) {
        return this.myService.getUser()
    }

    @UseGuards(AuthGuard("jwt"))
    @Get("friend")
    getFriend(@Request() req) {
        // return this.friend.allFriend()
        // return this.friend.validate
        // console.log(req)
        return req.user
    }

    // 员工注册
    @Post("staffRegister")
    staffRegister(@Body() body){

       return this.myService.staffRegister(body)
    }

    //员工身份设置
    @Post("staffIdentity")
    staffIdentity(@Body() body){
        
        body.employeeNo = this.utils.randomNo()
       
       return this.myService.setIdentity(body)
       
    }

    // 员工登录
    @Get("staffLogin")
    staffLogin(@Body() body,@Query() que){
        if(body.username){
            // if(!body.username && !body.password)return {code:400,mes:"用户名或密码不能为空喔~"}
            return  this.myService.staffLogin(body.username,body.password)
        }else if(que.username){
            return  this.myService.staffLogin(que.username,que.password)
        }else{
            if(!body.username && !body.password)return {code:400,mes:"用户名或密码不能为空喔~"}
        }
        // if(q)
      
     
    }


}
